package com.luxshare.aos.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.luxshare.aos.R;

import java.text.MessageFormat;

public class ArrayListAdapterActivity extends Activity {
    private static int count = 0;
    private int temp = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arrayadapter);

        ListView listView = (ListView) findViewById(R.id.ListView_Main);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{
                "ArrayListAdapter",
                "LayoutInflater",
                "SimpleAdapter",
                "Service",
                "WriteSDCARD",
                "ReadSDCARD",
                "ContentProvider"}));
        listView.setItemsCanFocus(true);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    TextView textView = (TextView) view;
                    String text = textView.getText().toString();
                    Intent intent = new Intent(ArrayListAdapterActivity.this, Class.forName(MessageFormat.format("com.luxshare.aos.demo.{0}Activity", text)));
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    Log.e(ArrayListAdapterActivity.class.getName(), "加载Activity Class失败！");
                }
            }
        });
        count++;
        temp = count;
        Log.i(ArrayListAdapterActivity.class.getName(), "onCreate/" + temp);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(ArrayListAdapterActivity.class.getName(), "onStart/" + temp);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(ArrayListAdapterActivity.class.getName(), "onRestart/" + temp);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(ArrayListAdapterActivity.class.getName(), "onResume/" + temp);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(ArrayListAdapterActivity.class.getName(), "onPause/" + temp);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(ArrayListAdapterActivity.class.getName(), "onStop/" + temp);
        if (count > temp)
            finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(ArrayListAdapterActivity.class.getName(), "onDestroy/" + temp);
    }

}